<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>活动中心 - 校园社团平台</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.2/font/bootstrap-icons.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
        <div class="container">
            <a class="navbar-brand" href="index.html">
                <i class="bi bi-people-fill"></i> 校园社团平台
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link" href="index.html">首页</a></li>
                    <li class="nav-item"><a class="nav-link" href="clubs.html">社团广场</a></li>
                    <li class="nav-item"><a class="nav-link active" href="activities.html">活动中心</a></li>
                    <li class="nav-item"><a class="nav-link" href="topics.html">话题广场</a></li>
                    <li class="nav-item"><a class="nav-link" href="circles.html">兴趣圈子</a></li>
                    <li class="nav-item" id="loginNav">
                        <a class="nav-link" href="login.html">登录</a>
                    </li>
                    <li class="nav-item d-none" id="userNav">
                        <div class="dropdown">
                            <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                                <i class="bi bi-person-circle"></i> <span id="username"></span>
                            </a>
                            <ul class="dropdown-menu dropdown-menu-end">
                                <li><a class="dropdown-item" href="profile.html">个人中心</a></li>
                                <li><a class="dropdown-item" href="my-clubs.html">我的社团</a></li>
                                <li><a class="dropdown-item" href="my-activities.html">我的活动</a></li>
                                <li><hr class="dropdown-divider"></li>
                                <li><a class="dropdown-item" href="#" id="logoutBtn">退出登录</a></li>
                            </ul>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container my-4">
        <div class="row mb-4">
            <div class="col-12">
                <h2><i class="bi bi-calendar-event"></i> 活动中心</h2>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body">
                        <h6 class="card-title"><i class="bi bi-funnel"></i> 筛选</h6>
                        <hr>
                        <div class="mb-3">
                            <label class="form-label">活动状态</label>
                            <select class="form-select" id="statusFilter">
                                <option value="">全部状态</option>
                                <option value="0" selected>报名中</option>
                                <option value="1">进行中</option>
                                <option value="2">已结束</option>
                            </select>
                        </div>
                        <button class="btn btn-primary w-100" id="searchBtn">
                            <i class="bi bi-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </div>

            <div class="col-md-9">
                <div id="activitiesList" class="row g-3">
                    <div class="col-12 text-center py-5">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">加载中...</span>
                        </div>
                    </div>
                </div>

                <div id="pagination" class="mt-4"></div>
            </div>
        </div>
    </div>

    <footer class="bg-dark text-white py-4 mt-5">
        <div class="container text-center">
            <p class="mb-0">&copy; 2024 校园社团与兴趣圈层平台. All rights reserved.</p>
        </div>
    </footer>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/js/bootstrap.bundle.min.js"></script>
    <script src="js/common.js"></script>
    <script>
        let currentPage = 1;
        const pageSize = 9;

        $(document).ready(function() {
            loadActivities();

            $('#searchBtn').on('click', function() {
                currentPage = 1;
                loadActivities();
            });
        });

        function loadActivities() {
            const status = $('#statusFilter').val();

            request.get('/activities', {
                page: currentPage,
                size: pageSize,
                status: status || undefined
            })
            .done(function(res) {
                if (res.code === 200) {
                    renderActivities(res.data.records);
                    renderPagination(res.data.total, res.data.size, res.data.current);
                } else {
                    $('#activitiesList').html('<div class="col-12 text-center text-danger">加载失败</div>');
                }
            })
            .fail(function() {
                $('#activitiesList').html('<div class="col-12 text-center text-danger">加载失败，请稍后重试</div>');
            });
        }

        function renderActivities(activities) {
            if (activities.length === 0) {
                $('#activitiesList').html('<div class="col-12 text-center text-muted py-5">暂无活动</div>');
                return;
            }

            const html = activities.map(activity => `
                <div class="col-md-4">
                    <div class="card h-100 shadow-sm">
                        ${activity.cover ? `<img src="${activity.cover}" class="card-img-top activity-cover" alt="${activity.title}">` : 
                          `<div class="card-img-top activity-cover bg-info d-flex align-items-center justify-content-center text-white">
                              <i class="bi bi-calendar-event" style="font-size: 3rem;"></i>
                          </div>`}
                        <div class="card-body">
                            <h5 class="card-title">${activity.title}</h5>
                            <p class="card-text text-muted small mb-2">
                                <i class="bi bi-building"></i> ${activity.clubName || '未知社团'}
                            </p>
                            <p class="card-text small mb-2">
                                <i class="bi bi-geo-alt"></i> ${activity.location || '待定'}
                            </p>
                            <p class="card-text small mb-2">
                                <i class="bi bi-calendar"></i> ${utils.formatDate(activity.startTime)}
                            </p>
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="badge bg-${utils.getStatusClass(activity.status)}">${utils.getStatusText(activity.status)}</span>
                                <small class="text-muted">
                                    <i class="bi bi-people"></i> ${activity.currentParticipants}/${activity.maxParticipants}
                                </small>
                            </div>
                        </div>
                        <div class="card-footer bg-transparent border-0">
                            <a href="activity-detail.html?id=${activity.id}" class="btn btn-sm btn-outline-primary w-100">
                                <i class="bi bi-eye"></i> 查看详情
                            </a>
                        </div>
                    </div>
                </div>
            `).join('');

            $('#activitiesList').html(html);
        }

        function renderPagination(total, size, current) {
            const totalPages = Math.ceil(total / size);
            
            if (totalPages <= 1) {
                $('#pagination').html('');
                return;
            }

            let html = '<nav><ul class="pagination justify-content-center">';
            
            html += `<li class="page-item ${current <= 1 ? 'disabled' : ''}">
                <a class="page-link" href="#" data-page="${current - 1}">上一页</a>
            </li>`;
            
            for (let i = 1; i <= totalPages; i++) {
                if (i === 1 || i === totalPages || (i >= current - 2 && i <= current + 2)) {
                    html += `<li class="page-item ${i === current ? 'active' : ''}">
                        <a class="page-link" href="#" data-page="${i}">${i}</a>
                    </li>`;
                } else if (i === current - 3 || i === current + 3) {
                    html += '<li class="page-item disabled"><span class="page-link">...</span></li>';
                }
            }
            
            html += `<li class="page-item ${current >= totalPages ? 'disabled' : ''}">
                <a class="page-link" href="#" data-page="${current + 1}">下一页</a>
            </li>`;
            
            html += '</ul></nav>';
            
            $('#pagination').html(html);
            
            $('#pagination a').on('click', function(e) {
                e.preventDefault();
                const page = parseInt($(this).data('page'));
                if (page && page !== currentPage) {
                    currentPage = page;
                    loadActivities();
                    $('html, body').scrollTop(0);
                }
            });
        }
    </script>
</body>
</html>

